package com.hyt.it.ogt.kq.service.gov.arrange;

import com.hyt.it.ogt.kq.common.config.exception.KqException;
import com.hyt.it.ogt.kq.common.gov.constant.ResponseCode;
import com.hyt.it.ogt.kq.common.gov.enums.ProctorArrangeParamEnum;
import com.hyt.it.ogt.kq.service.gov.arrange.model.Proctor;
import com.hyt.it.ogt.kq.service.gov.arrange.model.Room;
import com.hyt.it.ogt.kq.service.gov.model.entity.TimeExaminer;
import com.hyt.it.ogt.kq.service.gov.model.entity.TimeRoomExaminer;
import com.hyt.it.ogt.kq.service.gov.model.param.proctor.AutoProctorParam;
import com.hyt.progress.service.ProgressManager;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentLinkedDeque;

@Service
public class ProctorArrangeByLevelAdapter extends ProctorArrangeAbstractService {
    @Override
    public void arrange(AutoProctorParam param) {
        ProgressManager.updateProgress(5L);
        // 要编排的区域层级
        int level = this.getLevel(param, ProctorArrangeParamEnum.ARRANGE_SELF_LEVEL);
        // 要编排的监考老师数量
        int teacherNum = this.getTeacherNum(param);
        ProgressManager.updateProgress(10L);

        // 把监考员按照级别分类好，已过滤不符合编排级别的监考员
        Map<String, ConcurrentLinkedDeque<Proctor>> proctorMap = this.getProctorList(param.getTaskId(), level);

        if (proctorMap.isEmpty()) {
            throw new KqException(ResponseCode.PROCTOR_ARRANGE_NOT_MATCH.getCode(), ResponseCode.PROCTOR_ARRANGE_NOT_MATCH.getMsg());
        }

        ProgressManager.updateProgress(25L);
        // 获取所有考场
        List<Room> dataList = timeRoomCandidateMapper.getProctorArrangeRoomList(param.getTaskId());
        ProgressManager.updateProgress(45L);

        Map<String, TimeExaminer> timeExaminerMap = new HashMap<>();
        List<TimeRoomExaminer> timeRoomExaminerList = new ArrayList<>();

        for (Room e : dataList) {
            String key = null;

            // 获取考场属于第几级
            int pLevel = 0;
            if (StringUtils.isNotBlank(e.getDistrict())) {
                pLevel = 3;
            } else if (StringUtils.isNotBlank(e.getCity())) {
                pLevel = 2;
            } else if (StringUtils.isNotBlank(e.getProvince())) {
                pLevel = 1;
            }

            // 用前端传的级别 构造预设中的key
            if (level == 1 && pLevel >= 1) { // 一级编排
                key = e.getProvince();
            } else if (level == 2 && pLevel >= 2) { // 二级编排
                key = StringUtils.joinWith("_", e.getProvince(), e.getCity());
            } else if (level == 3 && pLevel == 3) { // 三级编排
                key = StringUtils.joinWith("_", e.getProvince(), e.getCity(), e.getDistrict());
            }

            if (key == null) {
                continue;
            }

            // 拿到同级别的监考员列表
            ConcurrentLinkedDeque<Proctor> proctors = proctorMap.get(key);
            // 判空null，没有，则该考场跳过不编排
            if (proctors == null || proctors.isEmpty()) {
                continue;
            }

            // 把监考员编进考场，构造表数据对象
            this.buildData(param, teacherNum, timeExaminerMap, timeRoomExaminerList, e, proctors);
        }
        ProgressManager.updateProgress(85L);
        if (timeRoomExaminerList.size() > 0) {
            iTimeRoomExaminerService.saveBatch(timeRoomExaminerList, 1000);
            iTimeExaminerService.saveBatch(timeExaminerMap.values(), 1000);
            ProgressManager.updateProgress(96L);
        } else {
            throw new KqException(ResponseCode.PROCTOR_ARRANGE_NOT_MATCH.getCode(), ResponseCode.PROCTOR_ARRANGE_NOT_MATCH.getMsg());
        }

    }

    /**
     * 把监考员按要编排的级别分组
     * @param taskId
     * @param level
     * @return
     */
    private Map<String, ConcurrentLinkedDeque<Proctor>> getProctorList(String taskId, int level) {
        Map<String, ConcurrentLinkedDeque<Proctor>> map = new HashMap<>();
        // 先查询所有监考员
        List<Proctor> dataList = examinerMapper.getProctorList(taskId);
        for (Proctor p : dataList) {
            String key = null;

            // 获取监考员属于第几级
            int pLevel = 0;
            if (StringUtils.isNotBlank(p.getDistrict())) {
                pLevel = 3;
            } else if (StringUtils.isNotBlank(p.getCity())) {
                pLevel = 2;
            } else if (StringUtils.isNotBlank(p.getProvince())) {
                pLevel = 1;
            }

            // 用前端传的级别 构造预设中的key
            if (level == 1 && pLevel >= 1) { // 一级编排
                key = p.getProvince();
            } else if (level == 2 && pLevel >= 2) { // 二级编排
                key = StringUtils.joinWith("_", p.getProvince(), p.getCity());
            } else if (level == 3 && pLevel == 3) { // 三级编排
                key = StringUtils.joinWith("_", p.getProvince(), p.getCity(), p.getDistrict());
            }

            // key是空，代表当前监考员不符合编排条件
            if (key == null) {
                continue;
            }

            // 符合，则存储该监考员到这一组中
            ConcurrentLinkedDeque<Proctor> list = map.get(key);
            if (list == null) {
                list = new ConcurrentLinkedDeque<>();
                map.put(key, list);
            }
            list.add(p);
        }
        return map;
    }

}
